﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web.Component;
using VioletDevelopmentLaboratory.Net.Web.Configuration;

namespace VioletDevelopmentLaboratory.Net.Web.WebControls
{
	public abstract class BoardSearchWebControlBase : ComponentWebControlBase
	{
		#region Properties
		[Browsable(true), Category("Design"), DefaultValue("default")]
		public String Template
		{
			get
			{
				return VUtility.GetControlSetting(ViewState["Template"] != null ? ViewState["Template"].ToString() : "", "BoardSearch.Template", "default");
			}
			set { ViewState["Template"] = value; }
		}

		[Browsable(true), Category("Design"), DefaultValue("default")]
		public String Stylesheet
		{
			get
			{
				return VUtility.GetControlSetting(ViewState["Stylesheet"] != null ? ViewState["Stylesheet"].ToString() : "", "BoardSearch.Stylesheet", "default");
			}
			set { ViewState["Stylesheet"] = value; }
		}

		[Browsable(true), Category("Behavior"), DefaultValue(10)]
		public Int32 PageSize
		{
			get
			{
				return Int32.Parse(VUtility.GetControlSetting(ViewState["PageSize"] != null ? ViewState["PageSize"].ToString() : "", "BoardSearch.PageSize", "10"));
			}
			set { ViewState["PageSize"] = value; }
		}

		[Browsable(true), Category("Behavior"), DefaultValue(10)]
		public Int32 PageButtonCount
		{
			get
			{
				return Int32.Parse(VUtility.GetControlSetting(ViewState["PageButtonCount"] != null ? ViewState["PageButtonCount"].ToString() : "", "BoardSearch.PageButtonCount", "10"));
			}
			set { ViewState["PageButtonCount"] = value; }
		}
		#endregion

		

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			//템플릿 에러
			if (!System.IO.Directory.Exists(Server.MapPath(VUtility.WebApplicationVirtualPath + "vboard/component/board/template_search/" + Template)))
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("General_InvalidTemplate"), "~/", true, 3);

			//스타일시트 에러
			if (!System.IO.Directory.Exists(Server.MapPath(VUtility.WebApplicationVirtualPath + "vboard/component/board/stylesheet_search/" + Stylesheet)))
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("General_InvalidStylesheet"), "~/", true, 3);

			#region 스타일시트 연결
			Literal styleLiteral = new Literal();
			styleLiteral.ID = "VWeb_Skin_StyleSheet";
			styleLiteral.Text = String.Format("<link rel=\"Stylesheet\" type=\"text/css\" href=\"{0}\" />", VUtility.WebApplicationVirtualPath + "vboard/component/board/stylesheet_search/" + Stylesheet + "/stylesheet.css");
			this.Page.Header.Controls.Add(styleLiteral);
			#endregion

			BoardSearchPartBase boardSearchResultControl = (BoardSearchPartBase)this.LoadControl(VUtility.WebApplicationVirtualPath + "vboard/component/board/template_search/" + Template + "/list.ascx");

			boardSearchResultControl.Page.Items["CurrentUserInfo"] = GetVBoardUserInfoBridge().GetUserInfo();
			boardSearchResultControl.Page.Items["Template"] = Template;
			boardSearchResultControl.Page.Items["Stylesheet"] = Stylesheet;

			boardSearchResultControl.Page.Items["PageSize"] = PageSize;
			boardSearchResultControl.Page.Items["PageButtonCount"] = PageButtonCount;

			TemplatePlaceHolder.Controls.Add(boardSearchResultControl);
		}
	}
}
